package com.ruoyi.business.utils;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * sqlSessionFactory缓存
 */
@Component
public class SqlSessionFactoryCache {
    private Map<String,SqlSessionFactory> cacheMap = new HashMap<>();

    /**
     * 放入缓存
     * @param key
     * @param sqlSessionFactory
     */
    public  void put(String key, SqlSessionFactory sqlSessionFactory){
        synchronized(cacheMap) {
            cacheMap.put(key, sqlSessionFactory);
        }
    }

    /**
     * 从缓存读取
     * @param key
     * @return
     */
    public  SqlSessionFactory get(String key){
        synchronized(cacheMap) {
            return cacheMap.get(key);
        }
    }

    /**
     * 删除数据源
     * @param key
     * @return
     */
    public  SqlSessionFactory remove(String key){
        synchronized(cacheMap) {
            return cacheMap.remove(key);
        }
    }
    /**
     * 构造key
     *
     */
    public String getKey(String enterpriseCode, String applicationCode,String dataSourceName){
        return dataSourceName+"_"+applicationCode+"_"+enterpriseCode;
    }
}
